package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.MesGetMat;
import com.ruoyi.system.domain.MesStock;
import com.ruoyi.system.mapper.MesStockMapper;
import com.ruoyi.system.service.IMesStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MesFininMapper;
import com.ruoyi.system.domain.MesFinin;
import com.ruoyi.system.service.IMesFininService;

/**
 * 完工入库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@Service
public class MesFininServiceImpl implements IMesFininService 
{
    @Autowired
    private MesFininMapper mesFininMapper;

    @Autowired
    private IMesStockService mesStockService;

    @Autowired
    private MesStockMapper mesStockMapper;

    /**
     * 查询完工入库
     * 
     * @param id 完工入库主键
     * @return 完工入库
     */
    @Override
    public MesFinin selectMesFininById(Integer id)
    {
        return mesFininMapper.selectMesFininById(id);
    }

    /**
     * 查询完工入库列表
     * 
     * @param mesFinin 完工入库
     * @return 完工入库
     */
    @Override
    public List<MesFinin> selectMesFininList(MesFinin mesFinin)
    {
        return mesFininMapper.selectMesFininList(mesFinin);
    }

    /**
     * 新增完工入库
     * 
     * @param mesFinin 完工入库
     * @return 结果
     */
    @Override
    public int insertMesFinin(MesFinin mesFinin)
    {
        List<MesFinin> mtrs = mesFinin.getMtrs();

        for (MesFinin mtr : mtrs) {
            //1：处理数据
            mtr.setTaskId(mtr.getTaskId());
            mtr.setFinInTime(mtr.getFinInTime());
            mtr.setProdId(mtr.getProdId());
            mtr.setProdNum(mtr.getProdNum());
            mtr.setLocaId(mtr.getLocaId());
            //2.sql插入
            mesFininMapper.insertMesFinin(mtr);

            //3.更新库存数量
            MesStock stock = mesStockMapper.selectByItemId(mtr.getProdId());
            if (stock != null){
                stock.setStockNum(stock.getStockNum() + mtr.getProdNum());
                mesStockService.updateMesStock(stock);
            }
        }
        return 1;
        //return mesFininMapper.insertMesFinin(mesFinin);
    }

    /**
     * 修改完工入库
     * 
     * @param mesFinin 完工入库
     * @return 结果
     */
    @Override
    public int updateMesFinin(MesFinin mesFinin)
    {
        return mesFininMapper.updateMesFinin(mesFinin);
    }

    /**
     * 批量删除完工入库
     * 
     * @param ids 需要删除的完工入库主键
     * @return 结果
     */
    @Override
    public int deleteMesFininByIds(Integer[] ids)
    {
        return mesFininMapper.deleteMesFininByIds(ids);
    }

    /**
     * 删除完工入库信息
     * 
     * @param id 完工入库主键
     * @return 结果
     */
    @Override
    public int deleteMesFininById(Integer id)
    {
        return mesFininMapper.deleteMesFininById(id);
    }
}
